package com.example.questionnaire.controller;

import cn.hutool.json.JSONObject;
import com.example.questionnaire.common.ServerResponse;
import com.example.questionnaire.exception.RException;
import com.example.questionnaire.service.AppidAndSecretService;
import com.example.questionnaire.service.QuestionTitleService;
import com.example.questionnaire.util.CommonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.Date;



@RestController
@RequestMapping("/QuestionTitle")
@Api(tags = "问卷题目")
public class QuestionTitleController {

    @Autowired
    private QuestionTitleService questionTitleService;
    @Autowired
    AppidAndSecretService appidAndSecretService;

    /**
     *创建及修改填空题
     * @param formid (问卷id)
     * @param title (题目)
     * @param subjectId (题号)
     * @param isMustAnswer (是否必答)
     * @param isWritePrompt (是否开启填写提示)
     * @param tipContent (提示内容)
     * @param multilineText (是否开启多行文本)
     * @param attribute (属性验证)
     * @param isSpaceConstraints (是否开启字数限制)
     * @param maxNum (字数限制最大值)
     * @param minNum (字数限制最小值)
     * @param isNotAllowedToRepeat (是否开启不允许重复)
     * @param questionsJumpItems (跳题id)
     * @return
     * @throws ParseException
     */
    @PostMapping("/updateAddFillInQuestion")
    @ApiOperation(value = "创建修改填空题", response = ServerResponse.class, httpMethod = "POST")
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class, RException.class})
    public ServerResponse updateAddFillInQuestion(@RequestParam String formid,
                                                  @RequestParam String title,
                                                  @RequestParam String subjectId,
                                                  @RequestParam boolean isMustAnswer,
                                                  @RequestParam boolean isWritePrompt,
                                                  @RequestParam String tipContent,
                                                  @RequestParam int multilineText,
                                                  @RequestParam String attribute,
                                                  @RequestParam boolean isSpaceConstraints,
                                                  @RequestParam String maxNum,
                                                  @RequestParam String minNum,
                                                  @RequestParam boolean isNotAllowedToRepeat,
                                                  @RequestParam String questionsJumpItems) throws ParseException {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader("token");
        if(appidAndSecretService.tokenTimeout(token)){
            String msg = "登录已过期";
            return ServerResponse.createBySuccess(10010,msg, null);

        }


        boolean b = questionTitleService.updateAddFillInQuestion(formid, title, subjectId, isMustAnswer, isWritePrompt, tipContent, multilineText, attribute, isSpaceConstraints, maxNum, minNum, isNotAllowedToRepeat, questionsJumpItems);
        if (b){
            return ServerResponse.createBySuccess("设置成功");
        }
        return ServerResponse.createByError("设置失败");
    }

    /**
     *创建及修改单选题
     * @param formid (问卷id)
     * @param title (题目)
     * @param subjectId (题号)
     * @param isMustAnswer (是否必答)
     * @param isWritePrompt (是否开启填写提示)
     * @param tipContent (填写提示内容)
     * @param jumpingType (跳题类型)
     * @param questionsJumpItems (题目跳题的id)
     * @param numberOfOptions (排列形式)
     * @param optionArray (选项数据)
     * @param groupingArray (分组数据)
     * @param isGroup (是否开启分组)
     * @return
     * @throws ParseException
     * @throws IOException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    @PostMapping("/updateAddMultipleChoiceQuestion")
    @ApiOperation(value = "创建修改单选题", response = ServerResponse.class, httpMethod = "POST")
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class, RException.class})
    public ServerResponse updateAddMultipleChoiceQuestion(@RequestParam String formid,
                                                          @RequestParam String title,
                                                          @RequestParam String subjectId,
                                                          @RequestParam Boolean isMustAnswer,
                                                          @RequestParam Boolean isWritePrompt,
                                                          @RequestParam String tipContent,
                                                          @RequestParam String jumpingType,
                                                          @RequestParam String questionsJumpItems,
                                                          @RequestParam String numberOfOptions,
                                                          @RequestParam String optionArray,
                                                          @RequestParam String groupingArray,
                                                          @RequestParam Boolean isGroup) throws ParseException, IOException, InvocationTargetException, IllegalAccessException {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader("token");

        if(appidAndSecretService.tokenTimeout(token)){
            String msg = "登录已过期";
            return ServerResponse.createBySuccess(10010,msg, null);

        }
        boolean b = questionTitleService.updateAddMultipleChoiceQuestion(formid,title,subjectId, isMustAnswer,isWritePrompt,tipContent,jumpingType,questionsJumpItems,numberOfOptions,optionArray,groupingArray,isGroup);
        if (b){
            return ServerResponse.createBySuccess("添加成功");
        }
        return ServerResponse.createByError("修改成功");
    }

    /**
     *创建及修改多选题
     * @param formid（问卷id）
     * @param title（题目）
     * @param subjectId（题号）
     * @param isGroup（是否开启分组）
     * @param isSort（是否开启排序）
     * @param isMustAnswer（是否必答）
     * @param isWritePrompt（是否开启填写提示）
     * @param tipContent（填写提示内容）
     * @param questionsJumpItems（题目跳题的id）
     * @param numberOfOptions（排列形式）
     * @param optionArray（选项数据）
     * @param most（最多选择项）
     * @param mini（最少选择项）
     * @param groupingArray（分组数据）
     * @return
     * @throws ParseException
     * @throws IOException
     */
    @PostMapping("/updateAddDoubleChoiceQuestion")
    @ApiOperation(value = "创建修改多选题", response = ServerResponse.class, httpMethod = "POST")
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class, RException.class})
    public ServerResponse updateAddDoubleChoiceQuestion(@RequestParam String formid,
                                                        @RequestParam String title,
                                                        @RequestParam String subjectId,
                                                        @RequestParam boolean isGroup,
                                                        @RequestParam boolean isSort,
                                                        @RequestParam boolean isMustAnswer,
                                                        @RequestParam boolean isWritePrompt,
                                                        @RequestParam String tipContent,
                                                        @RequestParam String questionsJumpItems,
                                                        @RequestParam String numberOfOptions,
                                                        @RequestParam String optionArray,
                                                        @RequestParam int most,
                                                        @RequestParam int mini,
                                                        @RequestParam String groupingArray) throws ParseException, IOException {

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader("token");
        if(appidAndSecretService.tokenTimeout(token)){
            String msg = "登录已过期";
            return ServerResponse.createBySuccess(10010,msg, null);

        }

        //返回题目id和问卷id
        boolean b = questionTitleService.updateAddDoubleChoiceQuestion(formid, title, subjectId, isGroup, isSort, isMustAnswer, isWritePrompt, tipContent, questionsJumpItems, numberOfOptions, optionArray, most, mini, groupingArray);
        if (b){
            return ServerResponse.createBySuccess("添加成功");
        }
        return ServerResponse.createBySuccess("修改成功");
    }

    /**
     * 删除题目
     * @param formid（问卷id）
     * @param subjectId（题号）

     */
    @PostMapping("/DeleteQuestion")
    @ApiOperation(value = "删除题目", response = ServerResponse.class, httpMethod = "POST")
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class, RException.class})
    public ServerResponse DeleteQuestion(@RequestParam String formid, @RequestParam String subjectId) throws RException, ParseException {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader("token");
        if(appidAndSecretService.tokenTimeout(token)){
            String msg = "登录已过期";
            return ServerResponse.createBySuccess(10010,msg, null);

        }
        boolean b = questionTitleService.DeleteQuestion(formid, subjectId);
        if (b){
            return ServerResponse.createBySuccess("删除成功");
        }
        return ServerResponse.createByError("删除失败");
    }

    /**
     * 题目跳题
     * @param subjectId（题号）

     */
    @GetMapping("/jumpquestiontitle")
    @ApiOperation(value = "题目跳题", response = ServerResponse.class, httpMethod = "GET")
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class, RException.class})
    public ServerResponse jumpquestiontitle(@RequestParam String subjectId) throws ParseException {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader("token");
        if(appidAndSecretService.tokenTimeout(token)){
            String msg = "登录已过期";
            return ServerResponse.createBySuccess(10010,msg, null);

        }
        return ServerResponse.createBySuccess(questionTitleService.jumpquestiontitle(subjectId));

    }

    /**
     * 获取点击问卷下所有题目
     * @param formid（问卷id）

     */
    @GetMapping("/getallquestions")
    @ApiOperation(value = "获取点击问卷下所有题目及题目信息", response = ServerResponse.class, httpMethod = "GET")
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class, RException.class})
    public ServerResponse getallquestions(@RequestParam String formid) throws ParseException {

        return ServerResponse.createBySuccess(questionTitleService.getallquestions(formid));

    }

//    /**
//     * 获取题目信息
//     * @param subjectId(题号)
//     * @return
//     * @throws RException
//     */
//    @GetMapping("/getAllInformation")
//    @ApiOperation(value = "获取题目信息", response = ServerResponse.class, httpMethod = "GET")
//    @Transactional(rollbackFor = {Exception.class, RuntimeException.class, RException.class})
//    public ServerResponse getAllInformation(@RequestParam int subjectId) throws RException {
//
//        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//        String token = request.getHeader("token");
////        if(CheckTokenExpiration(token)){
////            String msg = "登录已过期";
////            return ServerResponse.createBySuccess(10010,msg, null);
////
////        }
//        return ServerResponse.createBySuccess(questionTitleService.getAllInformation(subjectId));
//
//    }

}